//
//  WelcomeViewController.swift
//  MyWeibo
//
//  Created by 余丽丽 on 15/10/12.
//  Copyright © 2015年 余丽丽. All rights reserved.
//

import UIKit
import SDWebImage

class WelcomeViewController: UIViewController {

    private var iconBottomCons: NSLayoutConstraint?
    
    override func viewDidLoad() {
        super.viewDidLoad()
        //测试欢迎界面
//        view.backgroundColor = UIColor.orangeColor()
        prepareUI()
        
        // 加载用户头像
        if let urlString = UserAccount.loadAccount()?.avatar_large{
        
            iconView.sd_setImageWithURL(NSURL(string: urlString))
        }
        
        //加载用户name
        if let name = UserAccount.loadAccount()?.name{
            label.text = name + "\n\n" + label.text!
        
        }
        
        // Do any additional setup after loading the view.
    }
    override func viewDidAppear(animated: Bool) {
        super.viewDidAppear(animated)
        // 提示：修改约束不会立即生效，添加了一个标记，统一由自动布局系统更新约束
        iconBottomCons?.constant = UIScreen.mainScreen().bounds.height - iconBottomCons!.constant
        UIView.animateWithDuration(1.2, delay: 0, usingSpringWithDamping: 0.8, initialSpringVelocity: 5.0, options: UIViewAnimationOptions(rawValue: 0), animations: { () -> Void in
            
            // 强制更新约束
            self.view.layoutIfNeeded()
            
            }) { (_) -> Void in
                NSNotificationCenter.defaultCenter().postNotificationName(YLLRootViewControllerSwitchNotification, object: true)
        }
        
    }
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
// 设置UI
    private func prepareUI() {
        view.addSubview(backImageView)
        view.addSubview(iconView)
        view.addSubview(label)
    
        //自动布局
        // 自动布局
        // 1> 背景图片
        backImageView.translatesAutoresizingMaskIntoConstraints = false
        view.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|-0-[subview]-0-|", options: NSLayoutFormatOptions(rawValue: 0), metrics: nil, views: ["subview": backImageView]))
        view.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|-0-[subview]-0-|", options: NSLayoutFormatOptions(rawValue: 0), metrics: nil, views: ["subview": backImageView]))
        
        // 2> 头像
        iconView.translatesAutoresizingMaskIntoConstraints = false
        view.addConstraint(NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: view, attribute: NSLayoutAttribute.CenterX, multiplier: 1.0, constant: 0))
        view.addConstraint(NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1.0, constant: 90))
        view.addConstraint(NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1.0, constant: 90))
        view.addConstraint(NSLayoutConstraint(item: view, attribute: NSLayoutAttribute.Bottom, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.Bottom, multiplier: 1.0, constant: 160))
        // 记录底边约束
        iconBottomCons = view.constraints.last
        
        // 3> 标签
        label.translatesAutoresizingMaskIntoConstraints = false
        view.addConstraint(NSLayoutConstraint(item: label, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.CenterX, multiplier: 1.0, constant: 0))
        view.addConstraint(NSLayoutConstraint(item: label, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.Bottom, multiplier: 1.0, constant: 16))
    }
    
    // MARK: - 懒加载控件
    private lazy var backImageView:UIImageView = UIImageView(image: UIImage(named: "ad_background"))
    private lazy var iconView:UIImageView = {
        let icon = UIImageView(image: UIImage(named: "avatar_default_big"))
        icon.layer.masksToBounds = true
        icon.layer.cornerRadius = 45
        return icon
    }()
    
    private lazy var label:UILabel = {
        let label = UILabel()
        label.text = "🎉欢迎回家!🎉"
        label.numberOfLines = 3
        label.textAlignment = NSTextAlignment.Center
        label.sizeToFit()
        return label
    }()
    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    */

}
